前提条件 已安装0.1.49及以上版本的Go SDK。更多信息,请参见安装Go SDK。 注意事项本示例以华东1(杭州)的公网Endpoint为例,其公网Endpoint为https://cn-hangzhou.log.aliyuncs.com。如果您希望通过与Project同地域的其他阿里云产品访问日志服务,请使用格式为https://cn-hangzhou-intranet.log.aliyuncs.com的私网Endpoint。关于日志服务支持的地域与Endpoint的对应关系,请参见服务入口。 示例代码本示例中,通过调用阿里云日志服务Go SDK中的相关API实现对事件库(EventStore)的管理,包括创建、更新、列出、获取和删除操作。 package main
import (
"fmt"
"os"
"strings"
"github.com/aliyun/aliyun-log-go-sdk"
)
var (
//日志服务的服务入口。此处以杭州为例,其它地域请根据实际情况填写。
endpoint = "cn-hangzhou.log.aliyuncs.com"
//日志服务的Project名称。
project = "ali-test-project"
//日志服务的事件库(EventStore)名称。
eventStoreName = "test-events"
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
// 此处以把AccessKey 和 AccessKeySecret 保存在环境变量为例说明。您可以根据业务需要,保存到配置文件里。
// 强烈建议不要把 AccessKey 和 AccessKeySecret 保存到代码里,会存在密钥泄漏风险。
client = sls.CreateNormalInterface(
endpoint,
os.Getenv("ACCESS_KEY_ID"),
os.Getenv("ACCESS_KEY_SECRET"),
"",
)
)
//创建事件库(EventStore)
func createEventStore() {
eventStore := &sls.LogStore{
Name: eventStoreName,
TTL: 30,
ShardCount: 2,
AutoSplit: true,
MaxSplitShard: 64,
}
if err := client.CreateEventStore(project, eventStore); err != nil {
panic(err)
}
fmt.Println(fmt.Sprintf("Create eventStore %s succcess", eventStoreName))
}
//更新事件库(EventStore)
func updateEventStore() {
eventStore := &sls.LogStore{
Name: eventStoreName,
TTL: 60,
ShardCount: 2,
AutoSplit: false,
}
// 调用客户端的UpdateEventStore方法更新事件库。
if err := client.UpdateEventStore(project, eventStore); err != nil {
panic(err)
}
fmt.Println(fmt.Sprintf("Update eventStore %s succcess", eventStoreName))
}
//列出所有事件库(EventStore)
func listEventStores() {
// 调用客户端的ListEventStore方法列出所有事件库。
eventStores, err := client.ListEventStore(project, 0, 10)
if err != nil {
panic(err)
}
fmt.Println(fmt.Sprintf("List eventStores: %s", strings.Join(eventStores, ",")))
}
//获取指定事件库(EventStore)的详细信息
func getEventStore() {
// 调用客户端的GetEventStore方法获取事件库的详细信息。
eventStore, err := client.GetEventStore(project, eventStoreName)
if err != nil {
panic(err)
}
fmt.Println(fmt.Sprintf("Get eventStore %s success", eventStore.Name))
}
//删除事件库(EventStore)
func deleteEventStore() {
// 调用客户端的DeleteEventStore方法删除事件库。
if err := client.DeleteEventStore(project, eventStoreName); err != nil {
panic(err)
}
fmt.Println(fmt.Sprintf("Delete eventStore %s success", eventStoreName))
}
func main() {
createEventStore()
updateEventStore()
listEventStores()
getEventStore()
deleteEventStore()
}
|